<template>
  <div class="app-container">
    <el-table
      :data="record"
      border
      style="width: 100%"
    >
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80"
      />
      <el-table-column
        prop="title"
        label="标题"
        align="center"
        width="180"
      />
      <el-table-column
        prop="description"
        label="描述"
        align="center"
        width="260"
      />
      <el-table-column
        prop="midImg"
        align="center"
        label="中图预览"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.midImg"
            fit="fill"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="大图预览"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row['bigImg']"
            fit="fill"
          />
        </template>
      </el-table-column>

      <el-table-column width="150" align="center" label="操作">
        <template slot-scope="scope">
          <el-tooltip :hide-after="1" class="item" effect="dark" content="编辑" placement="top">
            <el-button type="primary" icon="el-icon-edit" circle @click="editBanner(scope.row)" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!--弹出框-->
    <el-dialog top="5vh" title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input
            v-model="form.description"
            maxlength="30"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <!--        中图-->
            <el-form-item label="首页中图" :label-width="formLabelWidth">
            <upload  v-model="form.midImg"></upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <!--        大图-->
            <el-form-item label="首页大图" :label-width="formLabelWidth">
            <upload   v-model="form.bigImg"></upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBanner, setBanner } from '@/api/banner'
import upload from '@/components/upload'
export default {
  name: 'Index',
  components: {
    upload
  },
  data() {
    return {
      record: null,
      formLabelWidth: '120',
      dialogFormVisible: false, // 弹出框的显示
      form: {
        id: 0,
        title: '',
        midImg: '',
        bigImg: '',
        description: ''
      }
    }
  },
  async created() {
    await this.fetchData()
  },
  methods: {
    async fetchData() {
      const data = await getBanner()
      this.record = data.data
    },
    editBanner(scope) {
      this.form = { ...scope }
      this.dialogFormVisible = true
    },
    async edit() {
      // 编辑确定按钮点击
      // 因为文档所以3个当作一个数组仍过去
      const arr = [...this.record].map((item) => {
        if (item.id === this.form.id) {
          item = this.form
        }
        return item
      })
      try {
        await setBanner(arr)
        this.dialogFormVisible = false
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        await this.fetchData()
      } catch (e) {
        this.$message({
          message: '修改失败',
          type: 'error'
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
